<template>
	<!-- 意向车型需求信息 -->
	<div>
		<FilterTable
			ref="table"
			height="300"
			:columns="columns"
			:dataSource="pdata.majorProjectModel.list"
			:isColumnFilter="false"
			:isSelectColumn="false"
			columnsRef='customerManage_approvalManage_approvalDetails_infoCar_01'
			:onColumnsChange="columns => this.columns = columns"
			:isToperInfo="false"
		>
			<template slot="controls" v-if="!isShow">
				<el-button size="small" type="primary" icon="el-icon-plus" @click="addContact">新增</el-button>
			</template>
		</FilterTable>
		<!-- 弹窗 -->
		<BaseDialog
			title="意向车型查询"
			:visible.sync="visibleDialog"
			destroyOnClose
			:containerStyle="{width:'50vm',height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
		>
			<product-madal ref="productMadal" @close="saveCloseDialog"/>
		</BaseDialog>
	</div>
</template>

<script>
	import { dictionary } from '@/utils/dictMixin';
	import { getProduct } from '@/api/cms-common/basedata';

	import productMadal from '@/pages/cms-common/product'

	export default {
		mixins: [dictionary],
		components: {
			productMadal
		},
		model:{
			prop:'pdata',
			event:'click'
		},
		props: {
			pdata: {
				type: Object,
				default: () => ({})
			},
			formType:{
				type: String,
				required:true
			}
		},
		data() {
			return {
				visibleDialog:false,
				productMap:{},
				columns: this.creatHeader(),
				isShow:(this.formType === "show"),
				nullProductMadalData:{
					materialId:'',
					productName:'',
					modelName:'',
					vehicleNum:'',
					vehicleNumIsEdit:true,
					planDate:'',
					planDateIsEdit:true,
					otherDesc:'',
					otherDescIsEdit:true,
				}
			};
		},
		async mounted(){
			//直接放在这里会出现回调比init()走的慢的问题，
			if(this.formType == 'add'){
				this.$refs.table.$refs.pageTable.loading = true;
				await this.getProductData();
				this.$refs.table.$refs.pageTable.loading = false
			}
			// this.getProductData();
		},
		methods: {
			//初始化数据
			async init() {
				//写在此处且同步执行，保证列表刷上去的时候this.productMap可以获取到数据
				this.$refs.table.$refs.pageTable.loading = true;
				await this.getProductData();
				this.pdata.majorProjectModel.list = this.pdata.allForm.majorProjectModelList;
				this.pdata.majorProjectModel.total = this.pdata.majorProjectModel.list.length
				let columnActionIndex = null;
				//赋值
				if(this.isShow){
					this.columns.forEach((x,index) => {
						x.editable = false;
						if(x.dataIndex === 'column-action'){
							columnActionIndex = index;
						}
					});
				}
				if(columnActionIndex != null){
					this.columns.splice(columnActionIndex,1);
				}
				this.$refs.table.$refs.pageTable.loading = false
			},
			async getProductData(){
				// await getProductApi({}).then(res=>{
				const res = await getProduct({pageNum:1,currentPage:1,pageSize:99999999,limit:99999999});
				if(res.resultCode === 200){
					let records = res.data.records;
					records.forEach(x => {
						let productId = 'data_' + (x.productId || '');
						this.productMap[productId] = x;
					});
				}
			},
			getProductDict(materialId){
				return this.productMap['data_' + materialId] || {};
			},
			getProductDictLabel(materialId,dataIndex){
				return (this.getProductDict(materialId) || {})[dataIndex] || '';
			},

			//打开添加窗口
			addContact(){
				this.visibleDialog = true;
			},
			//删除行方法
			deleteHandle(row){
				this.$confirm('是否确定删除吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async() => {
					this.pdata.majorProjectModel.list.splice(row.$index,1);
				}).catch(() => {});
			},
			//关闭添加窗口
			saveCloseDialog(data){
				if(!data.data){
					this.visibleDialog = data.visible; //关闭弹窗
					return ;
				}
				let dataList = data.data;
				// let rdataList = [];
				let oldModelList = this.pdata.majorProjectModel.list;
				// let $data = data.data[0];
				if(dataList && dataList.length){
					for(let $i in dataList){
						let $data = dataList[$i];
						let haveId = false;
						for(let i in oldModelList){
							if(oldModelList[i].materialId == $data.productId){
								haveId = true;
								//this.$notify.warning({title:'提示信息',message:'该车型已存在'});
								break;
							}
						}
						if(!haveId){

							let newData = Object.assign({},this.nullProductMadalData);
							newData.materialId = $data.productId;
							newData.productCode = $data.productCode;
							newData.productName = $data.productName;
							newData.modelName = $data.modelName;

							// rdataList.push(newData);
							// this.pdata.majorProjectModel.total = this.pdata.majorProjectModel.list.length
							this.pdata.majorProjectModel.list.push(newData);
						}

					}
					// this.pdata.majorProjectModel.list.concat(rdataList);
					// this.$set(this.pdata.majorProjectModel,'list',rdataList);
				}
				this.visibleDialog = data.visible; //关闭弹窗
			},
			saveDialog(){
				if(this.$refs.table.$refs.pageTable.loading){
					this.$notify.warning({title:'提示信息',message:'数据还在加载中。。。请耐心等待！'});
					return false;
				}
				if(this.$parent.submitType == 1){
					if(this.pdata.majorProjectModel.list.length <= 0){
						this.$notify.warning({title:'提示信息',message:'请添加意向车型需求信息'});
						return false;
					}
				}
				if(this.$refs.table.$refs.pageTable.GET_REQUIRED_ERROR() == null){
					//合并数据
					Object.assign(this.pdata.allForm.majorProjectModelList,this.pdata.majorProjectModel.list);
					this.pdata.allForm.majorProjectModelList.forEach(x => {
						if(String(x.planDate).indexOf("-01 00:00:00") == -1){
							x.planDate = x.planDate + "-01 00:00:00";
						}
					});
					this.$emit('formchange', this.pdata);
				}
			},
			//table表头和数据源
			creatHeader() {
				return [
					{
						title: '序号',
						dataIndex: 'index',
						width: 80,
						sorter: true,
						render: props => {
							return <span>{props.$index + 1}</span>;
						}
					},
					{
						title: '操作',
						dataIndex: 'column-action',
						width: 100,
						fixed: 'left',
						render: (props, h) => {
							return (
							<div>
								<el-button size="mini" type="text" onClick={() => this.deleteHandle(props.row)}>
									删除
								</el-button>
							</div>
							);
						}
					},
					{ title: '整车编码', dataIndex: 'productCode', sorter: true,
						render: props => {
							return <span>{this.getProductDictLabel(props.row.materialId,'productCode')}</span>;
						}
					},
					{ title: '产品名称', dataIndex: 'productName', sorter: true,
						render: props => {
							return <span>{this.getProductDictLabel(props.row.materialId,'productName')}</span>;
						}
					},
					{ title: '车型名称', dataIndex: 'modelName', sorter: true,
						render: props => {
							return <span>{this.getProductDictLabel(props.row.materialId,'modelName')}</span>;
						}
					},
					{ title: '数量（台）', dataIndex: 'vehicleNum', sorter: true, editable: true, editRequired:true, defaultEditable: true, editType: 'number',precision:0,max:99999999,min:0},
					{ title: '预计交车时间', dataIndex: 'planDate', dateFormat: 'yyyy-MM', sorter: true, editable: true, defaultEditable: true, dateType:'month', editType: 'date-dateFormat' },
					{ title: '其它情况说明', dataIndex: 'otherDesc', sorter: true, editable: true, defaultEditable: true, editType: 'text',maxlength:200}
				];
			}
		}
	};
</script>

<style lang="less" scoped>
</style>
